package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Attachment;
import com.ruoyi.system.domain.AttachmentType;
import com.ruoyi.system.domain.CourseAttachment;
import com.ruoyi.system.domain.vo.AttachmentVo;
import com.ruoyi.system.mapper.AttachmentMapper;
import com.ruoyi.system.mapper.AttachmentTypeMapper;
import com.ruoyi.system.mapper.CourseAttachmentMapper;
import com.ruoyi.system.service.AttachmentTypeService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 附件类型表(AttachmentType)表服务实现类
 *
 * @author makejava
 * @since 2024-07-28 10:23:23
 */
@Service("attachmentTypeService")
public class AttachmentTypeServiceImpl implements AttachmentTypeService {
    @Resource
    private AttachmentTypeMapper attachmentTypeMapper;

    @Resource
    private AttachmentMapper attachmentMapper;

    @Resource
    private CourseAttachmentMapper courseAttachmentMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AttachmentType queryById(Long id) {
        return this.attachmentTypeMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param attachmentType 筛选条件
     * @return 查询结果
     */
    @Override
    public List<AttachmentType> queryByPage(AttachmentType attachmentType) {
        return attachmentTypeMapper.queryAllByLimit(attachmentType);
    }

    /**
     * 新增数据
     *
     * @param attachmentType 实例对象
     * @return 实例对象
     */
    @Override
    public AttachmentType insert(AttachmentType attachmentType) {
        System.out.println(attachmentType.toString());
        this.attachmentTypeMapper.insert(attachmentType);
        return attachmentType;
    }

    /**
     * 修改数据
     *
     * @param attachmentType 实例对象
     * @return 实例对象
     */
    @Override
    public AttachmentType update(AttachmentType attachmentType) {
        this.attachmentTypeMapper.update(attachmentType);
        return this.queryById(attachmentType.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.attachmentTypeMapper.deleteById(id) > 0;
    }

    @Override
    public List<AttachmentVo> queryAllByCourseId(String courseId) {
        List<String> types = attachmentTypeMapper.queryAllByCourseId(courseId);
        List<AttachmentVo> attachmentVos = new ArrayList<>();
        for (String type : types) {
            List<Attachment> attachments =new  ArrayList<>();
            List<CourseAttachment> courseAttachments = courseAttachmentMapper.queryAllByCourseIdAndAttachmentType(courseId, type);
            for (CourseAttachment courseAttachment : courseAttachments) {
                Attachment attachment = attachmentMapper.queryById(courseAttachment.getAttachmentId());
                attachments.add(attachment);
            }
            attachmentVos.add(new AttachmentVo(type, attachments));
        }
        return attachmentVos;
    }
}
